package perso.marcr.lst.spring3.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.XmlMappingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.xml.sax.SAXException;

import perso.marcr.lst.model.Configuration;
import perso.marcr.lst.model.ScenarioList;
import perso.marcr.lst.service.ConfigurationService;
import perso.marcr.lst.service.CronService;
import perso.marcr.lst.service.JobService;
import perso.marcr.lst.service.TaskService;

@Controller
@SessionAttributes
@RequestMapping("/job")
public class TaskController extends ParentController implements InitializingBean{

	public TaskController() {
		super(TaskController.class);
	}

	@Autowired
	private ConfigurationService configurationService = null;

	@Autowired
	private CronService cronService = null;

	@Autowired
	private JobService jobService = null;

	@Autowired
	private TaskService taskService = null;

	@RequestMapping(value="/view/task-update", method = RequestMethod.POST)
	protected void viewTaskUpdate(RedirectAttributes model, String value, String jobName, String taskId)
	{
		try {
			ScenarioList scenarioList = taskService.read(jobName, taskId);
			scenarioList.setDescription(value);
			taskService.save(scenarioList, jobName, taskId);
		} catch (JAXBException e) {
			addError(e, model);
		} catch (XmlMappingException e) {
			addError(e, model);
		} catch (IOException e) {
			addError(e, model);
		}
	}

	@RequestMapping(value="/view/task-delete-{jobName}-{taskId}", method = RequestMethod.GET)
	protected String viewTaskDelete(RedirectAttributes model, @PathVariable String jobName, @PathVariable String taskId)
	{
		taskService.delete(jobName, taskId);
		return "redirect:/job/view/index-"+jobName+".html";
	}


	@RequestMapping(value="/view/testLaunchTask-{jobName}", method = RequestMethod.GET)
	protected String viewTestLaunchTask(RedirectAttributes model, @PathVariable String jobName)
	{
		// load configuration :
		try {			
			cronService.fireNow(jobName);
		}
		catch (SchedulerException e) {			
			addError(e, model);
		}

		return "redirect:/job/view/index-"+jobName+".html";
	}

	@RequestMapping(value="/view/testLoadTask-{jobName}-{taskId}", method = RequestMethod.GET)
	protected String viewTestLoadTask(Model model, @PathVariable String jobName, @PathVariable String taskId)
	{
		ScenarioList scenarioList = null;

		if(taskService.isRunning(jobName, taskId))
		{
			// currently running.
			scenarioList = taskService.getTaskListResults();
			model.addAttribute("running", true);
		}
		else
		{
			model.addAttribute("running", false);
			try {
				scenarioList = taskService.read(jobName, taskId);
			} catch (JAXBException e) {
				addError(e, model);
			}
		}		

		model.addAttribute("scenarioList", scenarioList);

		//only display it using rest api.
		return null;
	}	

	// PostConstruct: after java 1.7!
	//@PostConstruct
	protected void initCron() {
		List<String> listJobs = jobService.getAllJobs();
		for(int i=0; i<listJobs.size(); i++)
		{
			try {
				String jobName = listJobs.get(i);

				Configuration configuration = configurationService.load(jobName);
				String cronExpression = configuration.getCronExpression();

				cronService.updateCron(jobName, cronExpression);
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JAXBException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() {		
		initCron();		
	}
}
